package com.zw.pdm.module.report.controller;

import com.zw.pdm.annotation.RepeatCommit;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.rep.Change;
import com.zw.pdm.entity.rep.Plan;
import com.zw.pdm.entity.rep.Sheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.report.service.ReportListService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.zw.pdm.constant.Constant.*;

/**
 * @Description: 机械设计院 计划列表接口
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/report/list")
@SuppressWarnings("all")
public class ReportListController extends BaseController {

    @Autowired
    private SheetService sheetService;
    @Autowired
    private ReportListService reportListService;
    @Autowired
    private DictCommonService dictCommonService;

    @GetMapping
    @RequiresPermissions({"report:list:view"})
    public String sheetPage(Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
        return "report/list/page";
    }

    /** 提报计划提交  列表 */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"report:list:view"})
    public JsonResult sheetList(Sheet sheet) {
        Integer i = reportListService.countOrgByUserId();
        startPage();
        //List<Sheet> list = reportListService.findWithReportList(sheet, i);
        List<Sheet> list = reportListService.findWithReportList(sheet,i);
        return jsonResult(list);
    }

    /** 查看功能  */
    @GetMapping("/{id}")
    @RequiresPermissions({"report:list:view"})
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        Sheet sheet = sheetService.getSheetById(sheetId);
        model.addAttribute("process", sheetService.findVpBySheetId(sheetId));
        model.addAttribute("sheet", sheet);
        if (sheet.getSheetType() == 1) {
            model.addAttribute("plans", sheetService.findPlanBySheetId(sheetId));
            return "report/list/print";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", sheetService.findChangeBySheetId(sheetId));
            return "report/list/changePrint";
        }
    }

    /** 删除功能， 删除计划单， 包含计划单中的所有计划 */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"report:list:del"})
    public JsonResult delSheet(Integer id) {
        return jsonResult(reportListService.delSheetById(id));
    }

    /** 提交功能  提交计划单 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/commit")
    @RequiresPermissions({"report:list:commit"})
    public JsonResult commitSheet(Integer id) {
        return jsonResult(reportListService.commitSheetById(id));
    }

    /** 修改功能， 计划单修改页面 */
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"report:list:edit"})
    public String editSheet(@PathVariable("id") Integer sheetId, Model model) {
        Sheet sheet = sheetService.getSheetById(sheetId);
        model.addAttribute("parts", sheetService.findPartByProjectCode(sheet.getProCode(), "外观".equals(sheet.getMalType()) ? null: sheet.getMalType()));
        model.addAttribute("sheet", sheet);
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
        if (sheet.getSheetType() == 1) {
            model.addAttribute("plans", sheetService.findPlanBySheetId(sheetId));
            switch (sheet.getReportType()) {
                case 1:
                    return "report/list/outEdit";
                case 2:
                    model.addAttribute("assTypes", dictCommonService.findDictByGroupName(GROUP_ASSTYPE));
                    model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
                    return "report/list/assEdit";
                case 3:
                    model.addAttribute("qualities", dictCommonService.findDictByGroupName(GROUP_QUALITY));
                    model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
                    return "report/list/malEdit";
            }
        } else {
            model.addAttribute("plans", sheetService.findChangeBySheetId(sheetId));
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            switch (sheet.getReportType()) {
                case 1:
                    return "report/list/outChangeEdit";
                case 2:
                    model.addAttribute("assTypes", dictCommonService.findDictByGroupName(GROUP_ASSTYPE));
                    model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
                    return "report/list/assChangeEdit";
                case 3:
                    model.addAttribute("qualities", dictCommonService.findDictByGroupName(GROUP_QUALITY));
                    model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
                    return "report/list/malChangeEdit";
            }
        }
        return null;
    }

    /** 修改功能， 删除计划单中的单个计划 */
    @ResponseBody
    @PostMapping("/plan/del")
    @RequiresPermissions({"report:list:edit"})
    public JsonResult delPlan(Integer id) {
        return jsonResult(reportListService.delPlanById(id));
    }

    /** 修改功能， 上传附件 */
    @ResponseBody
    @PostMapping("/plan/upload")
    @RequiresPermissions({"report:list:edit"})
    public JsonResult planUpload(Plan plan, @RequestParam("file") MultipartFile file) {
        return isNotNull(reportListService.uploadWithPlan(plan, file));
    }

    /** 修改功能， 修改外购件计划 */
    @ResponseBody
    @PostMapping("/out/edit")
    @RequiresPermissions({"report:list:edit"})
    public JsonResult outEdit(Sheet sheet, Integer[] planId, String[] malDesc, Double[] num, String[] unit, String[] standard, String[] brand, String[] remark) {
        return jsonResult(reportListService.editOut(sheet, planId, malDesc, num, unit, standard, brand, remark));
    }

    /** 修改功能， 修改外协件计划 */
    @ResponseBody
    @PostMapping("/ass/edit")
    @RequiresPermissions({"report:list:edit"})
    public JsonResult assEdit(Sheet sheet, Integer[] planId, String[] malDesc, String[] diagram, Double[] num, String[] unit, String[] assistType, String[] remark) {
        return jsonResult(reportListService.editAss(sheet, planId, malDesc, diagram, num, unit, assistType, remark));
    }

    /** 修改功能， 修改原材料计划 */
    @ResponseBody
    @PostMapping("/mal/edit")
    @RequiresPermissions({"report:list:edit"})
    public JsonResult malEdit(Sheet sheet, Integer[] planId, String[] malDesc, Double[] num, String[] unit, String[] standard, String[] quality, String[] useStore, String[] remark) {
        return jsonResult(reportListService.editMal(sheet, planId, malDesc, num, unit, standard, quality, useStore, remark));
    }

    /** 变更功能， 选择计划单明细页面 */
    @GetMapping("/select/{id}")
    @RequiresPermissions({"report:list:cha"})
    public String select(@PathVariable("id") Integer sheetId, Model model) {
        model.addAttribute("sheet", sheetService.getSheetById(sheetId));
        return "report/list/selectPage";
    }

    /** 变更功能， 详细计划列表 */
    @ResponseBody
    @PostMapping("/planList/{id}")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult planList(@PathVariable("id") Integer sheetId) {
        startPage();
        List<Plan> list = reportListService.findPlanListBySheetId(sheetId);
        return jsonResult(list);
    }

    /** 变更功能， 详细计划列表 */
    @ResponseBody
    @PostMapping("/changeList/{id}")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult changeList(@PathVariable("id") Integer sheetId) {
        startPage();
        List<Change> list = reportListService.findChangeListBySheetId(sheetId);
        return jsonResult(list);
    }

    /** 变更功能， 变更页面 */
    @GetMapping("/changePage")
    @RequiresPermissions({"report:list:cha"})
    public String changePage(Integer sheetId, String ids, Model model) {
        Sheet sheet = sheetService.getSheetById(sheetId);
        model.addAttribute("sheet", sheet);
        if (sheet.getSheetType() == 1) {
            model.addAttribute("plans", reportListService.findPlanByIds(ids));
        }
        if (sheet.getSheetType() == 2) {
            model.addAttribute("plans", reportListService.findChangeByIds(ids));
        }
        model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
        switch (sheet.getReportType()) {
            case 1:
                return "report/list/outChange";
            case 2:
                model.addAttribute("assTypes", dictCommonService.findDictByGroupName(GROUP_ASSTYPE));
                model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
                return "report/list/assChange";
            case 3:
                model.addAttribute("qualities", dictCommonService.findDictByGroupName(GROUP_QUALITY));
                model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
                return "report/list/malChange";
        }
        return null;
    }

    /** 变更功能， 删除变更单的计划，前端暂时没做 */
    @ResponseBody
    @PostMapping("/change/del")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult delChange(Integer id) {
        return jsonResult(reportListService.delChangeById(id));
    }

    /** 变更功能， 创建变更计划单上传附件 */
    @ResponseBody
    @PostMapping("/out/change/save/upload")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult uploadTemp(@RequestParam("file") MultipartFile file) {
        String path = sheetService.uploadTemp(file);
        return success().setData(path);
    }

    /** 变更功能， 保存外购件计划变更单 */
    @ResponseBody
    @PostMapping("/out/change/save")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult saveOutChange(Sheet sheet,
                                    String[] changeReason, Integer[] planId,
                                    String[] afterMalDesc, Double[] afterNum, String[] afterUnit,
                                    String[] afterStandard, String[] afterBrand, String[] afterRemark, String[] afterUrl) {
        return jsonResult(reportListService.saveOutChange(sheet, changeReason,
                planId, afterMalDesc, afterNum, afterUnit, afterStandard,
                afterBrand, afterRemark, afterUrl));
    }

    /** 变更功能， 保存外购件计划变更单 */
    @ResponseBody
    @PostMapping("/ass/change/save")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult saveAssChange(Sheet sheet,
                                    String[] changeReason, Integer[] planId,
                                    String[] afterMalDesc, String[] afterDiagram, Double[] afterNum, String[] afterUnit,
                                    String[] afterAssistType, String[] afterRemark, String[] afterUrl) {
        return jsonResult(reportListService.saveAssChange(sheet, changeReason,
                planId, afterMalDesc, afterDiagram, afterNum, afterUnit, afterAssistType,
                afterRemark, afterUrl));
    }

    /** 变更功能， 保存外购件计划变更单 */
    @ResponseBody
    @PostMapping("/mal/change/save")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult saveMalChange(Sheet sheet,
                                    String[] changeReason, Integer[] planId,
                                    String[] afterMalDesc, Double[] afterNum, String[] afterUnit,
                                    String[] afterStandard, String[] afterQuality, String[] afterUseStore, String[] afterRemark, String[] afterUrl) {
        return jsonResult(reportListService.saveMalChange(sheet, changeReason,
                planId, afterMalDesc, afterNum, afterUnit, afterStandard, afterQuality,
                afterUseStore, afterRemark, afterUrl));
    }

    /** 变更修改功能， 上传变更附件 */
    @ResponseBody
    @PostMapping("/change/upload")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult changeUpload(Change change, @RequestParam("file") MultipartFile file) {
        return isNotNull(reportListService.uploadWithChange(change, file));
    }

    /** 变更修改功能， 修改外购件变更计划单 */
    @ResponseBody
    @PostMapping("/out/change/edit")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult outChangeEdit(Sheet sheet,
                              String[] changeReason, Integer[] planId,
                              String[] afterMalDesc, Double[] afterNum, String[] afterUnit,
                              String[] afterStandard, String[] afterBrand, String[] afterRemark) {
        return jsonResult(reportListService.editOutChange(sheet, planId, changeReason, afterMalDesc, afterNum, afterUnit, afterStandard, afterBrand, afterRemark));
    }

    /** 变更修改功能， 修改外购件变更计划单 */
    @ResponseBody
    @PostMapping("/ass/change/edit")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult assChangeEdit(Sheet sheet,
                                    String[] changeReason, Integer[] planId,
                                    String[] afterMalDesc, String[] afterDiagram, Double[] afterNum, String[] afterUnit,
                                    String[] afterAssistType, String[] afterRemark) {
        return jsonResult(reportListService.editAssChange(sheet, planId, changeReason, afterMalDesc, afterDiagram, afterNum, afterUnit, afterAssistType, afterRemark));
    }

    /** 变更修改功能， 修改外购件变更计划单 */
    @ResponseBody
    @PostMapping("/mal/change/edit")
    @RequiresPermissions({"report:list:cha"})
    public JsonResult malChangeEdit(Sheet sheet,
                                    String[] changeReason, Integer[] planId,
                                    String[] afterMalDesc, Double[] afterNum, String[] afterUnit,
                                    String[] afterStandard, String[] afterQuality, String[] afterUseStore, String[] afterRemark) {
        return jsonResult(reportListService.editMalChange(sheet, planId, changeReason, afterMalDesc, afterNum, afterUnit, afterStandard, afterQuality, afterUseStore, afterRemark));
    }

}
